//
//  YYPayVC.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/8/19.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYPayVC.h"

@interface YYPayVC ()
@property (nonatomic ,copy)NSString * is_pay;

@end

@implementation YYPayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.lbprice.text = [NSString stringWithFormat:@"￥%@",_money];
    _is_pay = @"0";
//    self.bgView.layer.shadowOffset = CGSizeMake(1, 1);
//    self.bgView.layer.shadowOpacity = 0.8;
//    self.bgView.layer.shadowColor = [UIColor colorWithWhite:0.7 alpha:1].CGColor;
    if (_isHidden) {
        self.lbTOp.hidden = YES;
        self.topHeight.constant = 16;
    }else{
         self.lbTOp.hidden = NO;
         self.topHeight.constant = 48;
    }
  
}

- (IBAction)aliyPay:(id)sender {
    [self.aliyBtn setImage:DEF_UIIMAGE_FROM_NAME(@"sign_inspection vehicle_checkbox") forState:UIControlStateNormal];
    [self.wechatBtn setImage:DEF_UIIMAGE_FROM_NAME(@"sign_inspection vehicle_checkbox_anselected") forState:UIControlStateNormal];
    _is_pay = @"0";
}
- (IBAction)wechatPay:(id)sender {
     [self.aliyBtn setImage:DEF_UIIMAGE_FROM_NAME(@"sign_inspection vehicle_checkbox_anselected") forState:UIControlStateNormal];
     [self.wechatBtn setImage:DEF_UIIMAGE_FROM_NAME(@"sign_inspection vehicle_checkbox") forState:UIControlStateNormal];
    _is_pay = @"1";
}
- (IBAction)pay:(id)sender {
    if (self.payBlock) {
        self.payBlock(_is_pay);
    }
    [self.navigationController popViewControllerAnimated:YES];

}

- (void)wechatPayWithResponse:(NSDictionary *)response {
    PayReq* req  = [[PayReq alloc] init];
    req.partnerId = response[@"partnerid"];
    req.prepayId= response[@"prepayid"];
    req.package = response[@"package"];
    req.nonceStr= response[@"noncestr"];
    req.timeStamp= [response[@"timestamp"] intValue];
    req.sign=response[@"sign"];
    [FLPAYMANAGER fl_payWithOrderMessage:req callBack:^(FLErrCode errCode, NSString *errStr, NSDictionary *resultDic) {
        
        if (errCode == FLErrCodeSuccess) {
            YYHintVC * hint = (YYHintVC *)[CFUtility getVC:@"YYHintVC" Storyboard:INSURANCE];
            hint.icon.image = DEF_IMAGENAME(@"sign_success");
            hint.lbTitle.text = @"支付成功";
            hint.lbContent.text = @"客服稍后会联系您，请保持电话畅通谢谢";
             [self.navigationController pushViewController:hint animated:YES];
        }else{
             [AlertUtil alertMessage:errStr];
        }
    }];
}

- (void)aliPayWithSign:(NSString * )sign{
    /**
     *  @author gitKong
     *
     *  来自支付宝文档数据
     */
   
    [FLPAYMANAGER fl_payWithOrderMessage:sign callBack:^(FLErrCode errCode, NSString *errStr, NSDictionary *resultDic) {
        if (resultDic&&[resultDic[@"resultStatus"] isEqualToString:@"9000"]) {
            YYHintVC * hint = (YYHintVC *)[CFUtility getVC:@"YYHintVC" Storyboard:INSURANCE];
                hint.icon.image = DEF_IMAGENAME(@"sign_success");
                hint.lbTitle.text = @"支付成功";
                hint.lbContent.text = @"客服稍后会联系您，请保持电话畅通谢谢";
            [self.navigationController pushViewController:hint animated:YES];
        }else{
            [AlertUtil alertMessage:errStr];
            
        }
        
    }];
    
    
}


- (NSString *)jumpToBizPay {
    //============================================================
    /**
     *  @author Clarence
     *
     *  来自微信文档数据
     */
    //============================================================
    NSString *urlString   = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
    //解析服务端返回json数据
    NSError *error;
    //加载一个NSURL对象
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    //将请求的url数据放到NSData对象中
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if ( response != nil) {
        NSMutableDictionary *dict = NULL;
        //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
        dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
        NSLog(@"url:%@",urlString);
        if(dict != nil){
            NSMutableString *retcode = [dict objectForKey:@"retcode"];
            if (retcode.intValue == 0){
                NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
                //调起微信支付
                PayReq* req             = [[PayReq alloc] init];
                req.partnerId           = [dict objectForKey:@"partnerid"];
                req.prepayId            = [dict objectForKey:@"prepayid"];
                req.nonceStr            = [dict objectForKey:@"noncestr"];
                req.timeStamp           = stamp.intValue;
                req.package             = [dict objectForKey:@"package"];
                req.sign                = [dict objectForKey:@"sign"];
                
                [FLPAYMANAGER fl_payWithOrderMessage:req callBack:^(FLErrCode errCode, NSString *errStr, NSDictionary *resultDic) {
                    NSLog(@"errCode = %zd,errStr = %@",errCode,errStr);
                }];
                //日志输出
                NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
                return @"";
            }else{
                return [dict objectForKey:@"retmsg"];
            }
        }else{
            return @"服务器返回错误，未获取到json对象";
        }
    }else{
        return @"服务器返回错误";
    }
    
}


@end
